package com.example.server.controller;


import com.example.server.bean.Admin;
import com.example.server.util.RespBean;
import com.example.server.bean.Role;
import com.example.server.service.IAdminRoleService;
import com.example.server.service.IAdminService;
import com.example.server.service.IRoleService;
import com.example.server.util.CurrentAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 别怿言
 * @since 2023-02-03
 */
@Api(tags = "AdminOperation")
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IAdminRoleService adminRoleService;

    @ApiOperation(value = "查询所有管理员")
    @GetMapping("/")
    public List<Admin> getAllAdmins(String Keyword){
        return adminService.getAllAdmins(Keyword);
    }

    @ApiOperation(value = "更新管理员信息")
    @PutMapping("/")
    public RespBean updateAdmin(@RequestBody Admin admin){
        if(adminService.updateById(admin)){
            return RespBean.success("更新信息成功！");
        }
        return RespBean.error("更新信息失败！");
    }

    @ApiOperation(value = "删除管理员信息")
    @DeleteMapping("/{id}")
    public RespBean deleteAdmin(@PathVariable Integer id){
        if(adminService.removeById(id)){
            return RespBean.success("删除信息成功！");
        }
        return RespBean.error("删除信息失败！");
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        return roleService.list();
    }

    @ApiOperation(value = "更改角色信息")
    @PutMapping("/role")
    public RespBean updateRole(Integer adminId,Integer[] ids){
        return adminRoleService.updateRole(adminId,ids);
    }
}
